//
//  HotRecommentTableViewCell.m
//  喜马拉雅FM
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "HotRecommentTableViewCell.h"

#import "ModelForData.h"
#import "UIImageView+WebCache.h"
#import "SecendViewController.h"
#import "ModelForListen.h"

@interface HotRecommentTableViewCell ()

@property (nonatomic, retain) UILabel *labelForTitle;
@property (nonatomic, retain) NSMutableArray *arrForPic;
@property (nonatomic, retain) NSMutableArray *arrForSubTitle;
@property (nonatomic, retain) NSMutableArray *arrForAlbumID;

@property (nonatomic, retain) NSMutableArray *arrForListenData;

@end

@implementation HotRecommentTableViewCell



- (void)dealloc {
    
    [_labelForTitle release];
    [_arrForPic release];
    [_arrForSubTitle release];
    [_arrForAlbumID release];
    [_arrForListenData release];
    [super dealloc];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.arrForPic = [NSMutableArray array];
        self.arrForSubTitle = [NSMutableArray array];
        self.arrForAlbumID = [NSMutableArray array];
        self.arrForListenData = [NSMutableArray array];
        [self createSubViews];
        [self createTapGestureRecognizer];
    }
    return self;
}

- (void)createSubViews {
    
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"biaoti"]];
    image.frame = CGRectMake(10, 15, 20, 20);
    [self.contentView addSubview:image];
    [image release];
    
    
    self.labelForTitle = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, 100, 50)];
    [self.contentView addSubview:self.labelForTitle];
    self.labelForTitle.text = @"听新闻";
    [self.labelForTitle release];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(WIDTH - 100, 0, 100, 50);
//    [button setTitle:@"更多 >" forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//    [self.contentView addSubview:button];
//    [button addTarget:self action:@selector(handleWebView:) forControlEvents:UIControlEventTouchUpInside];
    
    
    for (int i = 0; i < 3; i++) {
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
        image.frame = CGRectMake(((WIDTH - 40) / 3 + 10) * i + 10 , 60, (WIDTH - 40) / 3, (WIDTH - 40) /3 *4/3);
        [self.contentView addSubview:image];
        
        // image的交互打开.
        image.userInteractionEnabled = YES;
        
        image.tag = 100 + 10 * i;
        [image release];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(((WIDTH - 40) / 3 + 10) * i + 10, 60 + (WIDTH - 40) /3 *4/3, (WIDTH - 40) / 3, 50)];
        label.text = @"全国最大规模人口迁移又开场了";
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:label];
        label.tag = 200 + 10 * i;
        [label release];
    }
}


- (void)createTapGestureRecognizer {
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapAction:)];
    [[self.contentView viewWithTag:100] addGestureRecognizer:tap];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapAction:)];
    [[self.contentView viewWithTag:110] addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapAction:)];
    [[self.contentView viewWithTag:120] addGestureRecognizer:tap2];
    [tap2 release];
    [tap1 release];
    [tap release];
}


- (void)handleTapAction:(UITapGestureRecognizer *)tap {
    
    
    
    SecendViewController *secend = [[SecendViewController alloc]init];
    // 根据tap在哪个视图上来判断传值.
    if (tap.view == [self.contentView viewWithTag:100]) {
        NSLog(@"100");
        secend.stringForAlbumID = self.arrForAlbumID[0];
        secend.modelForListen = self.arrForListenData[0];
    }else if (tap.view == [self.contentView viewWithTag:110]){
        secend.stringForAlbumID = self.arrForAlbumID[1];
        secend.modelForListen = self.arrForListenData[1];
        NSLog(@"110");
    }
    else {
        secend.stringForAlbumID = self.arrForAlbumID[2];
        secend.modelForListen = self.arrForListenData[2];
        NSLog(@"120");
    }
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    [vc.navigationController pushViewController:secend animated:YES];
}


- (void)passModelForData:(ModelForData *)model {
    
    // 将要收藏的该model信息传到下一页.
    [self.arrForListenData removeAllObjects];
    NSArray *arr = [NSArray arrayWithArray:model.arrForHotRecommends];
    for (NSDictionary *dic in arr) {
        ModelForListen *modelForListen = [[ModelForListen alloc]init];
        [modelForListen setValuesForKeysWithDictionary:dic];
        [self.arrForListenData addObject:modelForListen];
    }
    
    
    
    // 给本页的布局赋值.
    self.labelForTitle.text = model.title;
    
    // 保证赋值之前数组中没有对象.
    [self.arrForPic removeAllObjects];
    [self.arrForSubTitle removeAllObjects];
    [self.arrForAlbumID removeAllObjects];
    
    for (NSDictionary *dic in model.arrForHotRecommends) {
        NSString *str1 = [dic objectForKey:@"coverLarge"];
        [self.arrForPic addObject:str1];
        
        str1 = [dic objectForKey:@"trackTitle"];
        [self.arrForSubTitle addObject:str1];
        
        str1 = [dic objectForKey:@"albumId"];
        [self.arrForAlbumID addObject:str1];
        
    }
    
    
    
    for (int i = 0 ;i < 3 ; i++) {
        UIImageView *image = [self.contentView viewWithTag:(100 + 10 * i)];
        [image sd_setImageWithURL:[NSURL URLWithString:self.arrForPic[i]]];
        
        UILabel *label = [self.contentView viewWithTag:(200 + 10*i)];
        label.text = self.arrForSubTitle[i];
    }
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
